/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package gui.objects3D;


import javax.media.j3d.Appearance;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.LineArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;


/**
 * The Class Rack3D.
 */
public class Rack3D extends Shape3D{
	
	/** The constants for the dimensions of the racks (depending on the loads). */
	public static final float LOADWIDTH = Load3D.LOADWIDTH;
	public static final float LOADLENGTH = Load3D.LOADLENGTH;
	public static final float LOADHEIGHT = Load3D.LOADHEIGHT;

	
	/**
	 * Instantiate a new Rack3D object.
	 * 
	 * @param points the calculated points for the 3D rack object
	 */
	public Rack3D(Point3f[] points){
		this.addGeometry(createGeometry(points));
		this.setAppearance(createAppearance());
	}

	
	/**
	 * Create appearance.
	 * 
	 * @return the appearance
	 */
	private Appearance createAppearance() {
		Appearance app = new Appearance();
		return app;
	}

	
	/**
	 * Create the geometry based on the calucated points.
	 * 
	 * @param points the calculated points for the 3D rack object
	 * @return the line array
	 */
	private LineArray createGeometry(Point3f[] points){
		// get calculated points from the array and associate them with variables to work with
		Point3f pa = points[0];
		Point3f pb = points[1];
		Point3f pc = points[2];
		Point3f pd = points[3];
		
		Point3f pe = points[4];
		Point3f pf = points[5];
		Point3f pg = points[6];
		Point3f ph = points[7];				
		
		float length = (pg.getZ() - pb.getZ()) / LOADWIDTH;
		float height = (pg.getY()-pc.getY()) / LOADHEIGHT;
		
		// calculate the number of vertex elements in this array
		int vertexElements = (int)((length+1)*8+(height+1)*8);
		// create the line array 
		LineArray gitter = new LineArray(vertexElements, GeometryArray.COORDINATES | LineArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2);
		
		// get initial positions
		float x = pa.getX();
		float y = pa.getY();
		float z = pa.getZ();
		
		int i;
		for(i = 0; i <= height*8; i+=8){
			// horizontal quadrangles
			Point3f p1 = new Point3f( x, y, z );
			Point3f p2 = new Point3f( x, y, z+(length*LOADWIDTH) );
			Point3f p3 = new Point3f( x+(LOADLENGTH), y,  z);
			Point3f p4 = new Point3f( x+(LOADLENGTH), y, z+(length*LOADWIDTH));
			// add quadrangles to the line array
			gitter.setCoordinate( i+0, p1);
			gitter.setCoordinate( i+1, p2);
			gitter.setCoordinate( i+2, p3);
			gitter.setCoordinate( i+3, p4);
			gitter.setCoordinate( i+4, p1);
			gitter.setCoordinate( i+5, p3);
			gitter.setCoordinate( i+6, p2);
			gitter.setCoordinate( i+7, p4);
			// increase the y-axis about the height of the load
			y += LOADHEIGHT;
		}
		
		// reset initial positions
		x = pa.getX();
		y = pa.getY();
		z = pa.getZ();

		for(int j = 0; j < (length+1); j++){
			// vertical quadrangles
			Point3f p1 = new Point3f( x, y, z );
			Point3f p2 = new Point3f( x+(LOADLENGTH), y,  z);
			Point3f p3 = new Point3f( x, y+(height*LOADHEIGHT), z );			
			Point3f p4 = new Point3f( x+(LOADLENGTH), y+(height*LOADHEIGHT), z);
			// add quadrangles to the line array
			gitter.setCoordinate( i+0, p1);
			gitter.setCoordinate( i+1, p2);
			gitter.setCoordinate( i+2, p3);
			gitter.setCoordinate( i+3, p4);
			gitter.setCoordinate( i+4, p1);
			gitter.setCoordinate( i+5, p3);
			gitter.setCoordinate( i+6, p2);
			gitter.setCoordinate( i+7, p4);
			// increase the z-axis about the height of the load
			z += LOADWIDTH;
			i += 8;
		}

		return gitter;
	}
	
}
